package com.magic.gre.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.magic.gre.R;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;
import com.noname.lib_base_java.net.download.DownloadInfo;

import java.io.File;
import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 19:35
 * 文件名: OffLineAdapter
 * 描述:
 */
public class OffLineAdapter extends CommonAdapter<DownloadInfo> {

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OffLineAdapter(Context mContext, List<DownloadInfo> mData) {
        super(mContext, mData);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(viewType, parent, false));
    }

    @Override
    protected void bindData(ViewHolder holder, int position) {
        TextView statusTv = holder.getView(R.id.status_tv);
        final DownloadInfo info = mData.get(position);

        File file = new File(info.getSavePath());

        if (file.exists()) {
            info.setReadLength(file.length());
        }

        holder.setText(R.id.title_tv, info.getThesaurus_name());

        holder.setText(R.id.hint1_tv, "音频包 " + getSize(info.getContentLength()));
        holder.setText(R.id.hint2_tv, "已下载 " + getSize(info.getReadLength()));


        if (info.getContentLength() == info.getReadLength()) {//下载完成
            statusTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            statusTv.setText("已下载");
            statusTv.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            statusTv.setEnabled(false);
        } else if (info.getReadLength() < info.getContentLength()) {//继续下载
            statusTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_download, 0, 0, 0);
            statusTv.setText(null);
            statusTv.setEnabled(true);
        } else if (info.getIsDownload() == 1) {
            statusTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            statusTv.setText("正在下载...");
            statusTv.setTextColor(Color.parseColor("#F4CD55"));
            statusTv.setEnabled(false);
        }

//        if (info.getIsDownload()==-1){
//            if (info.getIsDownload()==0){
//                statusTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//                statusTv.setText("已下载");
//                statusTv.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
//                statusTv.setEnabled(false);
//            }else if (info.getIsDownload()==1){
//                statusTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//                statusTv.setText("正在下载...");
//                statusTv.setTextColor(Color.parseColor("#F4CD55"));
//                statusTv.setEnabled(false);
//            }
//        }else {
//            if (info.getContentLength() == info.getReadLength()) {//下载完成
//                statusTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//                statusTv.setText("已下载");
//                statusTv.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
//                statusTv.setEnabled(false);
//            } else if (info.getReadLength() < info.getContentLength()) {//继续下载
//                statusTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_download, 0, 0, 0);
//                statusTv.setText(null);
//                statusTv.setEnabled(true);
//            }
//        }

        statusTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onDowanload(info);
                }
            }
        });

    }

    private String getSize(long contentLength) {

        float f = contentLength / 1024 / 1024;

        return String.valueOf(f) + "MB";
    }


    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return R.layout.item_off_line_one;
        } else {
            return R.layout.item_off_line_two;
        }
    }

    public interface OnItemClickListener {
        void onDowanload(DownloadInfo info);
    }
}
